package br.com.vti.sisp_sicrim.antigo.dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;

import br.com.vti.sisp_sicrim.antigo.dao.util.HibernateUtil;

public class DidEmissaoDAO {

	private int codigoRetorno = 0; // *ERROR
	private String msgRetorno = ""; // *ERROR
	public long qtdRegistros = 0; // *counter
	public long nroRegistro = 0; // *counter
	private String[] listacol = { "", "RegGeralEmitido", "TipReg", "DataEmissao", "NomeIdent", "NumeroSerieRg", "HoraEmissaoCarteira", "MatriculaFunc", "Numpos", "Ufeder" };
	private ResultSet query = null;

	public String insert(Long pRegGeralEmitido, Integer pTipReg, Integer pDataEmissao, String pNomeIdent, Long pNumeroSerieRg, Integer pHoraEmissaoCarteira, Integer pMatriculaFunc, Integer pNumpos, String pUfeder) {

		String sql = "{ ? = call sp_grava_Did_Emissao ( ?,?,?,?,?,?,?,?,? ) }";
		UUID pId;
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {

			CallableStatement stp = HibernateUtil.getConnection().prepareCall(sql);
			stp.registerOutParameter(1, Types.OTHER);
			stp.setLong(2, pRegGeralEmitido); // (getColuna("RegGeralEmitido")
			stp.setInt(3, pTipReg); // (getColuna("TipReg")
			stp.setInt(4, pDataEmissao); // (getColuna("DataEmissao")
			stp.setString(5, pNomeIdent); // (getColuna("NomeIdent")
			stp.setLong(6, pNumeroSerieRg); // (getColuna("NumeroSerieRg")
			stp.setInt(7, pHoraEmissaoCarteira); // (getColuna("HoraEmissaoCarteira")
			stp.setInt(8, pMatriculaFunc); // (getColuna("MatriculaFunc")
			stp.setInt(9, pNumpos); // (getColuna("Numpos")
			stp.setString(10, pUfeder); // (getColuna("Ufeder")
			// execute SP
			stp.execute();
			pId = (UUID) stp.getObject(1);
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
		return String.valueOf(pId);
	}

	public ResultSet readDataEmissaoin(Integer pDataEmissao1, Integer pDataEmissao2) {

		String sql = "select * from sp_leDataEmissaoFaixa_Did_Emissao (";
		sql = sql + "pDataEmissao1 := '" + String.valueOf(pDataEmissao1) + "',";
		sql = sql + "pDataEmissao2 := '" + String.valueOf(pDataEmissao2) + "' ";
		sql = sql + ")";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet readDataEmissaoinMatriculaFuncTipReg(Integer pDataEmissao1, Integer pDataEmissao2, Integer pMatriculaFunc, Integer pTipReg) {

		String sql = "select * from sp_leDataEmissaoFaixaMatriculaFuncTipReg_Did_Emissao (";
		sql = sql + "pDataEmissao1 := '" + String.valueOf(pDataEmissao1) + "',";
		sql = sql + "pDataEmissao2 := '" + String.valueOf(pDataEmissao2) + "',";
		sql = sql + "pMatriculaFunc := '" + String.valueOf(pMatriculaFunc) + "',";
		sql = sql + "pTipReg := '" + String.valueOf(pTipReg) + "' ";
		sql = sql + ")";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet readDataEmissaoinTipReg(Integer pDataEmissao1, Integer pDataEmissao2, Integer pTipReg) {

		String sql = "select * from sp_leDataEmissaoFaixaTipReg_Did_Emissao (";
		sql = sql + "pDataEmissao1 := '" + String.valueOf(pDataEmissao1) + "',";
		sql = sql + "pDataEmissao2 := '" + String.valueOf(pDataEmissao2) + "',";
		sql = sql + "pTipReg := '" + String.valueOf(pTipReg) + "' ";
		sql = sql + ")";
		codigoRetorno = 0;
		qtdRegistros = 0;
		nroRegistro = 0;
		msgRetorno = "";
		try {
			query = HibernateUtil.executarQuery(sql);
			if (query == null) {
				return null;
			} else {
				query.first();
				if (query.getRow() == 0) {
					query = null;
					return query;
				}
				if (query.first()) {
					query.last();
					qtdRegistros = query.getRow();
					nroRegistro = 1;
					query.first();
				}
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
		}

		return query;
	}

	public ResultSet pesquisaProximo() {
		// query
		try {
			if (query == null)
				return null;
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			query.next();
			if (query.getRow() == 0) {
				query = null;
				return null;
			}
			if (query != null) {
				nroRegistro++;
			}
			return query;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return null;
		}
	}

	public boolean temProximo() {
		try {
			if (query == null)
				return false;
			if (query.getRow() == 0) {
				return false;
			}
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			return false;
		}
		return (nroRegistro < qtdRegistros);
	}

	public void fecha() {
		if (query == null)
			return;
		try {
			query.close();
			query = null;
		} catch (SQLException e) {
			codigoRetorno = 999;
			msgRetorno = e.getMessage();
			query = null;
		}
	}

	public int getColuna(String coluna) {
		for (int i = 0; i < listacol.length; i++) {
			if (listacol[i].equals(coluna))
				return i;
		}
		return -1;
	}

}
